"use strict";
const passwordHash = require('yi-password-hash');
const async = require('async');
const ObjectID = require('mongodb').ObjectID;
const userAuth = require('yi-user-auth');

const middleware =(req, res, next)=>{

    next();
};

const handler =(req, res, next)=>{

    let mobile = req.body.mobile;
    let captcha = req.body.captcha;
    let qrCodeType = 'qrCodeType';

    req.redis.get("mobile:"+mobile,(e,r)=> {
        if (e) return res.err.redisError(e);
        if(!r) return res.reply({code:1004,message:'验证码已过期'});
        if(r.toString() !==captcha.toString()) return res.reply({code:1005,message:'验证码不正确'});

        req.mongo.collection('user')
            .find({ tel:mobile})
            .limit(1)
            .next(function (err, user) {
                if (err) return res.err.mongoError(err);
                if (!user){
                    return res.reply({code:1001,message:"手机号不存在"});
                }
                else{
                    user.role = 'user';
                    let qrCodeType = req.body.qrCodeType;
                    if(qrCodeType==='StoreLogin'){
                        req.mongo.collection('store').find({$or:[{userId:user._id},{members:user._id}]}).toArray((e1,r1)=>{
                            if(e1 || !r1 || r1.length===0){
                                return res.reply({code:10001,message:"该账号未注册商铺！"});
                            }

                            user.store = r1;
                            user.role = '';
                            userAuth(user,req,(e,r)=>{
                                if(e)
                                    res.err.unknownError(e1);
                                else{
                                    delete user.pwd;
                                    res.reply({code:0,message:"登录成功",data:r});
                                }
                            });
                        });

                    }else if(qrCodeType==='MallLogin'){
                        req.mongo.collection('mall').find({$or:[{userId:user._id},{members:user._id}]}).toArray((e2,r2)=>{
                            if(e2 || !r2 || r2.length===0){
                                return res.reply({code:10002,message:"该账号未注册商场！"});
                            }
                            user.mall=r2;
                            user.role = '';
                            userAuth(user,req,(e,r)=>{
                                if(e)
                                    res.err.unknownError(e);
                                else{
                                    delete user.pwd;
                                    res.reply({code:0,message:"登录成功",data:r});
                                }
                            });
                        });
                    }else{
                        userAuth(user,req,(e,r)=>{
                            if(e)
                                res.err.unknownError(e);
                            else{
                                delete user.pwd;
                                res.reply({code:0,message:"登录成功",data:r});
                            }
                        });
                    }
                }

            });

    });

};


module.exports = {
    //open:true,
    //security: true,
    description: '短信登录',
    middleware: middleware,
    handler: handler
    // handler(req, res, next) {
    //     const mobile = req.body.mobile;
    //     const captcha = req.body.captcha;
    //     let client = req.redis.client;
    //     let key = "mobile:"+mobile;
    //
    //     req.redis.get(key,(e,r)=>{
    //
    //     });
    // }
};
